package com.example.jwtdemo.exception;

import com.example.jwtdemo.exception.entity.Result;
import com.example.jwtdemo.exception.entity.ResultCode;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(CommonException.class)
    public Result handleCommonException(CommonException ex){
        ResultCode resultCode = ex.getResultCode();
        return new Result(resultCode);
    }

    /**
     * 空指针异常
     * @param ex NullPointerException
     * @return
     */
    @ExceptionHandler(NullPointerException.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public Result handleTypeMismatchException(NullPointerException ex) {
        return new Result(500, "空指针异常了",false);
    }
}
